Behavior Driven Development (BDD) হলো একটি সফটওয়্যার ডেভেলপমেন্ট পদ্ধতি, যা টেস্টিং এবং ডেভেলপমেন্টকে একত্রিত করে। BDD-তে, টেস্টগুলো ব্যবহারকারীর আচরণ (behavior) বা সিস্টেমের আউটপুটের ওপর ভিত্তি করে লেখা হয়, যা কোডিংয়ের সময় উন্নত যোগাযোগ ও স্পষ্টতা নিশ্চিত করে। JasmineJS একটি BDD ফ্রেমওয়ার্ক, যা এই পদ্ধতি অনুসরণ করে। Jasmine এর describe(), it(), beforeEach(), এবং afterEach() ফাংশনগুলি ব্যবহার করে আপনি সহজে BDD টেস্ট কেস তৈরি করতে পারেন।
এই টিউটোরিয়ালে, আমরা BDD Best Practices নিয়ে আলোচনা করব এবং কিভাবে JasmineJS ব্যবহার করে সেগুলি প্রয়োগ করা যায়, তার উদাহরণ দেখাবো।
BDD Best Practices
BDD-তে কার্যকরী টেস্ট লেখা এবং সফলভাবে টেস্টিং করার জন্য কিছু সেরা প্র্যাকটিস অনুসরণ করা গুরুত্বপূর্ণ। এই প্র্যাকটিসগুলো JasmineJS বা যে কোনও BDD ফ্রেমওয়ার্কে অ্যাপ্লিকেবল।
১. স্পষ্ট এবং বোধগম্য নাম ব্যবহার করুন
BDD তে, আপনার টেস্ট কেসের নাম ব্যবহারকারীর আচরণকে প্রতিফলিত করতে হবে। describe() এবং it() ব্লকে নামগুলো স্পষ্ট ও বোধগম্য হওয়া উচিত যাতে অন্য ডেভেলপার বা টেস্টার সহজেই বুঝতে পারে টেস্টটি কী চেক করছে।
উদাহরণ:
describe("User Login", function() {
it("should allow a user to log in with valid credentials", function() {
// Test logic
});
});
এখানে describe() ব্লকটি পুরো ফিচার বা ফাংশনালিটি বর্ণনা করছে, এবং it() ব্লকটি সুনির্দিষ্ট ব্যবহারকারীর আচরণ বা ফলাফল বর্ণনা করছে।
২. Given-When-Then প্যাটার্ন অনুসরণ করুন
BDD টেস্টে Given, When, Then প্যাটার্ন অনুসরণ করা একটি ভাল অভ্যাস। এটি টেস্ট কেসের আর্কিটেকচার স্পষ্ট করে এবং সহজে বুঝতে সাহায্য করে।
- Given: প্রারম্ভিক অবস্থা বা কনডিশন
- When: ঘটনার ট্রিগার
- Then: প্রত্যাশিত আউটপুট বা আচরণ
উদাহরণ:
describe("User Login", function() {
it("should show an error message if the user provides incorrect credentials", function() {
// Given: The user is on the login page
const usernameField = document.getElementById('username');
const passwordField = document.getElementById('password');
// When: The user submits the form with incorrect credentials
usernameField.value = "wrongUser";
passwordField.value = "wrongPassword";
submitLoginForm();
// Then: An error message should appear
const errorMessage = document.getElementById('error');
expect(errorMessage).not.toBeNull();
});
});
৩. টেস্টগুলো ছোট এবং ফোকাসড রাখুন
একটি টেস্ট শুধুমাত্র একটি ছোট এবং নির্দিষ্ট আচরণ পরীক্ষা করা উচিত। বৃহত্তর ফিচার বা ফাংশনালিটির জন্য একাধিক ছোট টেস্ট তৈরি করুন। এটি আপনাকে টেস্টগুলো সহজে পরিচালনা এবং ডিবাগ করতে সহায়ক হবে।
উদাহরণ:
describe("Addition Function", function() {
it("should add two positive numbers correctly", function() {
expect(add(2, 3)).toBe(5);
});
it("should return a negative number when adding a positive and a negative number", function() {
expect(add(-2, 3)).toBe(1);
});
});
৪. এন্ড-টু-এন্ড টেস্টিং এর জন্য সিমুলেটেড ইউজার অ্যাকশন ব্যবহার করুন
BDD স্টাইলে টেস্ট লেখার সময়, কখনও কখনও UI (User Interface) এর ওপর ভিত্তি করে end-to-end testing করা প্রয়োজন হয়। JasmineJS-এ DOM manipulation এবং ইউজার ইভেন্ট সিমুলেট করে টেস্ট করা সম্ভব।
উদাহরণ:
describe("Form Submission", function() {
it("should redirect the user to the dashboard after successful login", function() {
// Given: A user is on the login page
document.body.innerHTML = '<form id="loginForm"><input type="text" id="username"></form>';
// When: User submits the login form with valid credentials
document.getElementById('username').value = "validUser";
document.getElementById('loginForm').submit();
// Then: The user should be redirected to the dashboard
expect(window.location.href).toBe("https://example.com/dashboard");
});
});
৫. কোড এবং টেস্টগুলোকে মডুলার এবং পুনঃব্যবহারযোগ্য রাখুন
আপনার টেস্ট এবং কোডের মধ্যে পুনরাবৃত্তি এড়িয়ে যাওয়ার চেষ্টা করুন। সাধারণভাবে ব্যবহৃত ফাংশন বা কোডের অংশগুলোকে মডুলার করে রাখুন যাতে সেগুলো অন্য টেস্টে পুনঃব্যবহার করা যায়।
উদাহরণ:
describe("User Authentication", function() {
let user;
beforeEach(function() {
user = new User("testUser", "password123");
});
it("should authenticate the user with valid credentials", function() {
expect(user.authenticate("testUser", "password123")).toBe(true);
});
it("should fail to authenticate the user with invalid credentials", function() {
expect(user.authenticate("testUser", "wrongPassword")).toBe(false);
});
});
এখানে, beforeEach() ফাংশন ব্যবহার করে user অবজেক্টটি প্রতিটি টেস্টের আগে প্রস্তুত করা হচ্ছে।
BDD Examples in JasmineJS
এখানে JasmineJS এর কিছু সাধারণ BDD টেস্টিং উদাহরণ দেওয়া হলো:
১. ব্যবহারকারী লগইন পরীক্ষা
describe("Login Functionality", function() {
let user;
beforeEach(function() {
user = { username: "testUser", password: "password123" };
});
it("should successfully log in with valid credentials", function() {
const result = login(user.username, user.password);
expect(result).toBe("Login successful");
});
it("should show an error for invalid credentials", function() {
const result = login(user.username, "wrongPassword");
expect(result).toBe("Invalid credentials");
});
});
২. কার্টে পণ্য যোগ করা
describe("Shopping Cart", function() {
let cart;
beforeEach(function() {
cart = new ShoppingCart();
});
it("should add a product to the cart", function() {
cart.addProduct("Laptop", 1);
expect(cart.products.length).toBe(1);
expect(cart.products[0].name).toBe("Laptop");
});
it("should increase the quantity of a product if it already exists", function() {
cart.addProduct("Laptop", 1);
cart.addProduct("Laptop", 1);
expect(cart.products[0].quantity).toBe(2);
});
});
৩. ফর্ম ভ্যালিডেশন পরীক্ষা
describe("Form Validation", function() {
it("should show an error message when the email is empty", function() {
const form = new Form();
form.email = "";
expect(form.validateEmail()).toBe("Email is required");
});
it("should show an error message for an invalid email", function() {
const form = new Form();
form.email = "invalid-email";
expect(form.validateEmail()).toBe("Invalid email format");
});
});
সারাংশ
BDD এর জন্য JasmineJS একটি শক্তিশালী ফ্রেমওয়ার্ক, যা উন্নত টেস্টিং প্রক্রিয়া প্রদান করে। Jasmine এর describe(), it(), beforeEach(), এবং afterEach() ফাংশনগুলি ব্যবহার করে আপনি সহজেই BDD best practices অনুসরণ করে টেস্ট কেস লিখতে পারেন। স্পষ্ট এবং বোধগম্য নাম ব্যবহার, Given-When-Then প্যাটার্ন অনুসরণ, এবং ছোট ও ফোকাসড টেস্ট লেখা BDD প্রক্রিয়ার মূল অংশ। JasmineJS ব্যবহার করে আপনি এই প্র্যাকটিসগুলো সঠিকভাবে প্রয়োগ করে কোডের গুণগত মান এবং টেস্টের কার্যকারিতা বাড়াতে পারেন।
Read more